import Vue from 'vue'
import VueRouter from 'vue-router'
//引入Login组件
import Login from '../views/Login.vue'
//导入Home
import Home from '../views/Home.vue'
//引入HelloWorld
import HelloWorld from  '../views/HelloWorld'
//引入UserList
import UserList from  '../views/user/UserList'
//引入AdminList
import AdminList from  '../views/admin/AdminList'
//引入Checkedworlds
import Checkedworlds from  '../views/world/Checkedworlds'
//引入UncheckedWorlds
import UncheckedWorlds from  '../views/world/UncheckedWorlds'


Vue.use(VueRouter)

//解决(Avoided redundant navigation to current location) 路由地址重复报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

const routes = [
  //设置默认路径
  {
    path:"/",
    //redirect:重定向
    redirect:"/login"
  },
  {
    path:"/login",
    component:Login
  },
  {
    path:"/home",
    component:Home,
    redirect:"/hello",
    children:[
      {
        path:"/hello",
        component:HelloWorld
      },
      {
        path:"/user",
        component:UserList
      },
      {
        path:"/admin",
        component:AdminList
      },
      {
        path:"/checked",
        component:Checkedworlds
      },
      {
        path:"/unchecked",
        component:UncheckedWorlds
      },
    ]
  },
  
]

const router = new VueRouter({
    routes
})



//挂载路由导航守卫
router.beforeEach((to,from,next) =>{
    // to 要访问的
    // from 从哪访问
    // next 接着干嘛 next(url) 重定向到url上的next() 继续访问 to路径
    if(to.path=='/login') return next();
    //获取token
    const tokenFlag = window.sessionStorage.getItem('token');
    if(!tokenFlag) return next('/login');
    next();

})



export default router
